/*     */ package com.engine;
/*     */ 
/*     */ import java.io.BufferedReader;
/*     */ import java.io.DataInputStream;
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.FileWriter;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.PrintStream;
/*     */ import java.net.URI;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import javax.xml.parsers.DocumentBuilder;
/*     */ import javax.xml.parsers.DocumentBuilderFactory;
/*     */ import javax.xml.transform.Result;
/*     */ import javax.xml.transform.Transformer;
/*     */ import javax.xml.transform.TransformerFactory;
/*     */ import javax.xml.transform.dom.DOMSource;
/*     */ import javax.xml.transform.stream.StreamResult;
/*     */ import org.jdom.JDOMException;
/*     */ import org.jdom.input.SAXBuilder;
/*     */ import org.jdom.output.Format;
/*     */ import org.jdom.output.XMLOutputter;
/*     */ 
/*     */ public class XMLEngine
/*     */   implements Runnable
/*     */ {
/*  30 */   private String fileXML = "";
/*  31 */   private int counter = 0;
/*     */   private String[] layanan;
/*     */   private IXML _ixml;
/*     */ 
/*     */   public XMLEngine(String filename, int countLoket)
/*     */   {
/*  35 */     this.fileXML = filename;
/*  36 */     this.counter = countLoket;
/*  37 */     this.layanan = new String[this.counter];
/*  38 */     readLayanan();
/*  39 */     if (!new File(filename).exists())
/*     */       try
/*     */       {
/*  42 */         new Thread(this).start();
/*     */       }
/*     */       catch (Exception e) {
/*  45 */         e.printStackTrace();
/*     */       }
/*     */   }
/*     */ 
/*     */   public void setXML(IXML sqEngine)
/*     */   {
/*  53 */     this._ixml = sqEngine;
/*     */   }
/*     */ 
/*     */   public void run()
/*     */   {
/*     */     try
/*     */     {
/*  61 */       DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
/*  62 */       DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
/*     */ 
/*  65 */       org.w3c.dom.Document doc = docBuilder.newDocument();
/*  66 */       org.w3c.dom.Element rootElement = doc.createElement("datas");
/*  67 */       doc.appendChild(rootElement);
/*  68 */       for (int i = 0; i < this.counter; i++)
/*     */       {
/*  71 */         org.w3c.dom.Element item = doc.createElement("item");
/*  72 */         rootElement.appendChild(item);
/*     */ 
/*  74 */         org.w3c.dom.Element firstname = doc.createElement("Loket");
/*  75 */         int noLoket = i + 1;
/*  76 */         firstname.appendChild(doc.createTextNode(String.valueOf(noLoket)));
/*  77 */         item.appendChild(firstname);
/*     */ 
/*  79 */         org.w3c.dom.Element second = doc.createElement("No");
/*  80 */         second.appendChild(doc.createTextNode(String.valueOf(0)));
/*  81 */         item.appendChild(second);
/*     */ 
/*  83 */         org.w3c.dom.Element third = doc.createElement("Nama");
/*  84 */         third.appendChild(doc.createTextNode(this.layanan[i] != null ? this.layanan[i] : ""));
/*  85 */         item.appendChild(third);
/*     */       }
/*     */ 
/*  90 */       TransformerFactory transformerFactory = TransformerFactory.newInstance();
/*  91 */       Transformer transformer = transformerFactory.newTransformer();
/*     */ 
/*  93 */       transformer.setOutputProperty("indent", "yes");
/*     */ 
/*  95 */       DOMSource source = new DOMSource(doc);
/*     */ 
/*  97 */       File foFile = new File(this.fileXML);
/*  98 */       Result foResult = new StreamResult(foFile.toURI().getPath());
/*  99 */       transformer.transform(source, foResult);
/*     */ 
/* 105 */       this._ixml.initXML();
/*     */     }
/*     */     catch (Exception ex)
/*     */     {
/* 110 */       System.err.println(ex.getMessage());
/*     */     }
/*     */   }
/*     */ 
/*     */   private void getChildren(org.jdom.Element element, int ploket, int queue)
/*     */   {
/* 117 */     List childrenList = element.getChildren();
/* 118 */     Iterator itr = childrenList.iterator();
/*     */ 
/* 120 */     while (itr.hasNext())
/*     */     {
/* 122 */       org.jdom.Element childElement = (org.jdom.Element)itr.next();
/*     */ 
/* 124 */       if (childElement.getChildren() == null)
/*     */         continue;
/* 126 */       if (childElement.getName().equals("item"))
/*     */       {
/* 128 */         String loket = childElement.getChild("Loket").getText();
/* 129 */         if (loket.equals(String.valueOf(ploket)))
/*     */         {
/* 131 */           childElement.getChild("No").setText(String.valueOf(queue));
/* 132 */           break;
/*     */         }
/*     */       }
/* 135 */       getChildren(childElement, ploket, queue);
/*     */     }
/*     */   }
/*     */ 
/*     */   private void getChildren(org.jdom.Element element)
/*     */   {
/* 143 */     List childrenList = element.getChildren();
/* 144 */     Iterator itr = childrenList.iterator();
/*     */ 
/* 146 */     while (itr.hasNext())
/*     */     {
/* 148 */       org.jdom.Element childElement = (org.jdom.Element)itr.next();
/*     */ 
/* 150 */       if (childElement.getChildren() == null)
/*     */         continue;
/* 152 */       if (childElement.getName().equals("item"))
/*     */       {
/* 154 */         String loket = childElement.getChild("Loket").getText();
/* 155 */         String no = childElement.getChild("No").getText();
/* 156 */         this._ixml.Queue(Integer.parseInt(loket), Integer.parseInt(no));
/*     */       }
/* 158 */       getChildren(childElement);
/*     */     }
/*     */   }
/*     */ 
/*     */   public void init()
/*     */   {
/*     */     try
/*     */     {
/* 168 */       SAXBuilder builder = new SAXBuilder();
/* 169 */       File xmlFile = new File(this.fileXML);
/*     */ 
/* 171 */       org.jdom.Document doc = builder.build(xmlFile);
/* 172 */       org.jdom.Element rootNode = doc.getRootElement();
/* 173 */       getChildren(rootNode);
/*     */     } catch (IOException io) {
/* 175 */       io.printStackTrace();
/*     */     } catch (JDOMException e) {
/* 177 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   public void update(int loket, int queue)
/*     */   {
/*     */     try {
/* 184 */       SAXBuilder builder = new SAXBuilder();
/* 185 */       File xmlFile = new File(this.fileXML);
/*     */ 
/* 187 */       org.jdom.Document doc = builder.build(xmlFile);
/* 188 */       org.jdom.Element rootNode = doc.getRootElement();
/* 189 */       getChildren(rootNode, loket, queue);
/*     */ 
/* 191 */       XMLOutputter xmlOutput = new XMLOutputter();
/*     */ 
/* 194 */       xmlOutput.setFormat(Format.getPrettyFormat());
/* 195 */       xmlOutput.output(doc, new FileWriter(this.fileXML));
/*     */     } catch (IOException io) {
/* 197 */       io.printStackTrace();
/*     */     } catch (JDOMException e) {
/* 199 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   private void readLayanan()
/*     */   {
/*     */     try
/*     */     {
/* 209 */       FileInputStream fstream = new FileInputStream("layanan.txt");
/*     */ 
/* 211 */       DataInputStream in = new DataInputStream(fstream);
/* 212 */       BufferedReader br = new BufferedReader(new InputStreamReader(in));
/*     */ 
/* 215 */       int idx = 0;
/*     */       String strLine;
/* 216 */       while ((strLine = br.readLine()) != null)
/*     */       {
/*     */         String strLine;
/* 218 */         this.layanan[idx] = strLine;
/* 219 */         idx++;
/*     */       }
/*     */ 
/* 222 */       in.close();
/*     */     } catch (Exception e) {
/* 224 */       System.err.println("Error: " + e.getMessage());
/*     */     }
/*     */   }
/*     */ }

/* Location:           F:\16 2\SQ.jar
 * Qualified Name:     com.engine.XMLEngine
 * JD-Core Version:    0.6.0
 */